package com.tszs.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tszs.WeChatUtils.Params;
import com.tszs.biz.DistributorBiz;
import com.tszs.entity.Distributor;
import com.tszs.entity.DistributorExample;
import com.tszs.util.CallBack;
import com.tszs.util.Uuid;

import net.sf.json.JSONObject;

/**
 * @类名：DistributorController
 * @类描述：分销业务处理 @创建时间：2019-04-29 08:46:40
 * @author Administrator
 *
 */
@Controller
public class DistributorController {
	/**
	 * @distributorBiz
	 */
	@Autowired
	private DistributorBiz distributorBiz;
	
	/***
	 * 判断是否已经是分销商
	 * @author MRC
	 * @date 2019年5月30日下午5:11:27
	 * @param request
	 * @param response
	 */
	@RequestMapping("weChat/IsDistributor")
	public void IsDistributor(HttpServletRequest request,HttpServletResponse response) {
		String openid = request.getSession().getAttribute("openid")+"";
		
		//查询是否重复注册
		DistributorExample example=new DistributorExample();
		DistributorExample.Criteria ca= example.createCriteria();
		ca.andDOpenidEqualTo(openid);
		List<Distributor> selectByExample = distributorBiz.selectByExample(example);
		
		if (selectByExample.size() == 1) {
			new CallBack().no(response, selectByExample.get(0).getdDistributionIdentification(), "");
		} else {
			new CallBack().yes(response, "", "");
		}
	}

	/**
	 * @类名：InsertDistr
	 * @类描述：分销申请 @创建时间：2019-04-29 08:46:40
	 * @author Administrator
	 *
	 */
	@RequestMapping("weChat/insertDistr")
	public void InsertDistr(HttpServletRequest request, HttpServletResponse response) {
		String openid = request.getSession().getAttribute("openid") + "";

		// 查询是否重复注册
		DistributorExample example = new DistributorExample();
		DistributorExample.Criteria ca = example.createCriteria();
		ca.andDOpenidEqualTo(openid);
		List<Distributor> selectByExample = distributorBiz.selectByExample(example);

		if (selectByExample.size() == 1) {
			// 已经注册过
			new CallBack().no(response, "", "");
			return;
		}

		String dName = request.getParameter("name");
		Map<String, String> map = selectByExample((String) request.getSession().getAttribute("distributionId"));
		int c = insert(openid, map.get("getdSuperior"), map.get("dLevelIdentification"), dName);
		if (c > 0) {
			new CallBack().yes(response, "", "");
		} else {
			new CallBack().no(response, "", "");
		}
	}

	/**
	 * @申请成为分销
	 * @param openid
	 */
	public int insert(String openid, String Superior, String dLevelIdentification, String dName) {
		Distributor record = new Distributor();
		record.setdId(Uuid.getUUID());
		record.setdOpenid(openid);
		record.setdSuperior(Superior);
		record.setdStatic("0");
		record.setdCreate(Uuid.getTime());
		record.setdDistributionIdentification(Uuid.getUUID());
		record.setdType("1");
		record.setdName(dName);
		record.setdLevelIdentification(dLevelIdentification);
		return distributorBiz.insertSelective(record);
	}

	/**
	 * @查询父级ID逻辑进行判断分销等级添加
	 * @param DistributionIdentification
	 * @return
	 */
	public Map<String, String> selectByExample(String DistributionIdentification) {
		DistributorExample example = new DistributorExample();
		DistributorExample.Criteria ca = example.createCriteria();
		ca.andDDistributionIdentificationEqualTo(DistributionIdentification);
		List<Distributor> selectByExample = distributorBiz.selectByExample(example);
		Map<String, String> map = new HashMap<>();
		String dSuperior = "";
		if (selectByExample.size() == 1) {
			dSuperior = DistributionIdentification;
			if (selectByExample.get(0).getdLevelIdentification().equals("1")) {
				map.put("dLevelIdentification", "2");
			} else if (selectByExample.get(0).getdLevelIdentification().equals("2")) {
				map.put("dLevelIdentification", "3");
			}
		} else {
			map.put("dLevelIdentification", "1");
		}
		map.put("getdSuperior", dSuperior);
		return map;
	}
	/*	*//**
			 * @查询当前级别
			 * @param selectDLevelIdentification
			 * @return
			 *//*
			 * @RequestMapping("txr/selectDLevelIdentification") public void
			 * selectDLevelIdentification(HttpServletRequest
			 * request,HttpServletResponse response) { DistributorExample
			 * example=new DistributorExample();
			 * com.tszs.entity.DistributorExample.Criteria
			 * ca=example.createCriteria();
			 * ca.andDDistributionIdentificationEqualTo(request.getParameter(
			 * "DistributionIdentification")); ca.andDTypeEqualTo("1");
			 * ca.andDStaticEqualTo("0"); ca.andDLevelIdentificationBetween("1",
			 * "2"); List<Distributor>
			 * selectByExample=distributorBiz.selectByExample(example);
			 * if(selectByExample.size()>0) { new CallBack().yes(response, "",
			 * "");
			 * 
			 * }else { new CallBack().no(response, "", "");
			 * 
			 * } }
			 */

	/**
	 * @查询当前openid
	 * @param selectDLevelIdentification
	 * @return
	 */
	@RequestMapping("txr/selectopenid")
	public void selectopenid(HttpServletRequest request, HttpServletResponse response) {
		if (selectOpenidSizee(request.getSession().getAttribute("openid") + "").size() > 0) {
			new CallBack().yes(response, "", "");
		} else {
			new CallBack().no(response, "", "");
		}
	}

	/**
	 * @查询账号是否申请
	 * @param selectDLevelIdentification
	 * @return
	 */
	@RequestMapping("weChat/selectopenidTrueFalse")
	public Map<String, Object> selectopenidTrueFalse(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<>();
		if (selectOpenidSizee(request.getSession().getAttribute("openid") + "").size() > 0) {
			new CallBack().yes(response, "", "");
		} else {
			new CallBack().no(response, "", "");
		}
		return map;
	}

	/***
	 * @查询当前openid
	 * @param openid
	 * @param DistributionIdentification
	 * @return
	 */
	public List<Distributor> selectOpenidSizee(String openid) {
		DistributorExample example2 = new DistributorExample();
		com.tszs.entity.DistributorExample.Criteria ca2 = example2.createCriteria();
		ca2.andDOpenidEqualTo(openid);
		ca2.andDTypeEqualTo("1");
		List<Distributor> selectByExample1 = distributorBiz.selectByExample(example2);
		return selectByExample1;
	}

	/***
	 * @根据注册ID 找到对应详情
	 * @param selectopenidFindSize
	 * @return
	 */
	@RequestMapping("weChat/selectopenidFindSize")
	public void selectopenidFindSize(HttpServletRequest request, HttpServletResponse response) {
		DistributorExample example = new DistributorExample();
		com.tszs.entity.DistributorExample.Criteria ca = example.createCriteria();
		ca.andDOpenidEqualTo(request.getSession().getAttribute("openid") + "");
		List<Distributor> selectByExample = distributorBiz.selectByExample(example);
		Distributor curDis = selectByExample.get(0);
		Map<String, Object> map = new HashMap<>();
		map.put("url", Params.SysTemUrl + "WeChat/login.do?id=" + curDis.getdDistributionIdentification());
		map.put("Grade", curDis.getdLevelIdentification());
		map.put("name", curDis.getdName());
		DistributorExample example2 = new DistributorExample();
		DistributorExample.Criteria ca2 = example2.createCriteria();
		ca2.andDSuperiorEqualTo(curDis.getdDistributionIdentification());
		List<Distributor> selectByExample2 = distributorBiz.selectByExample(example2);
		map.put("subordinate", selectByExample2.size());//下级数量
		new CallBack().yes(response, map, "");
	}

	/**
	 * @方法名:findDistrListPage
	 * @方法描述：商品列表分页 @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 * @author Administrator
	 *
	 */
	@RequestMapping("pc/admin/findDistrListPage")
	public void findCommodityListPage(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("text/xml; charset=UTF-8");
		response.setContentType("text/html;charset=utf-8");
		int page = Integer.parseInt(request.getParameter("page"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		Distributor example = new Distributor();
		example.setPage((page - 1) * limit);
		example.setLimit(limit);
		List<Distributor> selectByExample = distributorBiz.selectByExamplePage2(example);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", selectByExample.size());
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < selectByExample.size(); i++) {
			Map<String, Object> mapData = new HashMap<String, Object>();
			mapData.put("id", i + 1);
			mapData.put("dOpenid", selectByExample.get(i).getdOpenid());
			mapData.put("dSuperior", selectByExample.get(i).getdSuperior());
			mapData.put("dCreate", selectByExample.get(i).getdCreate());
			mapData.put("dDistributionIdentification", selectByExample.get(i).getdDistributionIdentification());
			mapData.put("dLevelIdentification", selectByExample.get(i).getdLevelIdentification());
			if (selectByExample.get(i).getdType().equals("0")) {
				mapData.put("button",
						" <a href=\"javascript:table('0','" + selectByExample.get(i).getdId()
								+ "')\" class=\"layui-btn layui-btn-danger layui-btn-xs\" style=''>同意</a> <a href=\"javascript:table('2','"
								+ selectByExample.get(i).getdId() + "')\" class=\"layui-btn layui-btn-xs\">拒绝</a>");
			} else if (selectByExample.get(i).getdType().equals("1")) {
				mapData.put("button", "<a style='green;'>已同意</a>");
			} else {
				mapData.put("button", "<a style='color:red;'>已拒绝</a>");
			}

			list.add(mapData);
		}
		map.put("data", list);
		JSONObject json = JSONObject.fromObject(map);
		new CallBack().textObject(json, response);
	}

	/**
	 * @同意拒绝申请成为分销商
	 * @param request
	 * @param response
	 */
	@RequestMapping("pc/admin/agreeToRefuse")
	public void agreeToRefuse(HttpServletRequest request, HttpServletResponse response) {
		Distributor record = new Distributor();
		DistributorExample example = new DistributorExample();
		com.tszs.entity.DistributorExample.Criteria ca = example.createCriteria();
		ca.andDIdEqualTo(request.getParameter("dId"));
		if (request.getParameter("type").equals("0")) {
			record.setdType("1");
		} else {
			record.setdType("2");
		}
		int i = distributorBiz.updateByExampleSelective(record, example);
		if (i > 0) {
			new CallBack().yes(response, "", "");
		} else {
			new CallBack().no(response, "", "");
		}
	}
}